package com.fh.controller.system.weixin.wxutil;

import java.awt.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.fh.controller.system.weixin.resp.SNSUserInfo;
import com.fh.controller.system.weixin.resp.WeixinOauth2Token;

public class OAuthUtil {
	/**
	 * 获取网页授权凭证
	 * @param appId 
	 * @param appSecret
	 * @param code
	 * @return WeixinOauth2Token
	 */
	public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code){
		WeixinOauth2Token wat = null;
		//拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("SECRET", appSecret);
		requestUrl = requestUrl.replace("CODE", code);
		//获取网页授权凭证
		JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
		if(null != jsonObject){
			try {
				wat = new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
			}
		}
		return wat;
	}
	
	public static WeixinOauth2Token refreshOauth2AccessToken(String appId, String refreshToken){
		WeixinOauth2Token wat = null;
		//拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("REFRESH_TOKEN", refreshToken);
		//刷新网页授权凭证
		JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
		if(null != jsonObject){
			try {
				wat = new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
			}
		}
		return wat;
	}
	
	public static SNSUserInfo getSNSUserInfo(String accessToken, String openId){
		SNSUserInfo snsUserInfo = null;
		//拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
		requestUrl = requestUrl.replace("OPENID", openId);
		//通过网页授权获取用户信息
		JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
		if(null != jsonObject){
			try {
				snsUserInfo = new SNSUserInfo();
				//用户标示
				snsUserInfo.setOpenId(jsonObject.getString("openid"));
				//用户昵称
				snsUserInfo.setNickname(jsonObject.getString("nickname"));
				//性别（1：男；2：女；0：未知）
				snsUserInfo.setSex(jsonObject.getInt("sex"));
				//国家
				snsUserInfo.setCountry(jsonObject.getString("country"));
				//省份
				snsUserInfo.setProvince(jsonObject.getString("province"));
				//城市
				snsUserInfo.setCity(jsonObject.getString("city"));
				//用户头像
				snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
				//用户特权信息
				snsUserInfo.setPrivilegList(JSONArray.toList(jsonObject.getJSONArray("privilege"),List.class));
			} catch (Exception e) {
				snsUserInfo = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
			}
		}
		return snsUserInfo;
	}
	
	public static void main(String[] args) {
		String appId = "wxb7f6c9100ba0a6cc";
		String secret = "e8dcbbde25f54500058a6687324e3e3b";
		String code = "222";
		System.out.println(getOauth2AccessToken(appId,secret,code).getRefreshToken());
	}
}
